#include <vector>
#include <string>
#include <iostream>
#include "logger.h"
#include "friend.pb.h"
#include "mprpcapplication.h"

class FriendService : public Joy::FriendServiceRpc
{
public:
    std::vector<Joy::User> GetFriendList(uint32_t userid)
    {
        std::cout << "do getFriendList service" << std::endl;
        std::vector<Joy::User> friends;
        for(int i = 0; i < 5; ++i)
        {
            Joy::User user;
            user.set_name("zhang san" + std::to_string(i + 1));
            user.set_age(18 + i);
            user.set_sex(Joy::User::MAN);
            friends.push_back(user);
        }

        return friends;
    }

    // 重写基类方法
    void GetFriendList(::google::protobuf::RpcController* controller,
                       const ::Joy::GetFriendListRequest* request,
                       ::Joy::GetFriendListResponse* response,
                       ::google::protobuf::Closure* done)
    {
        uint32_t userid = request->userid();
        std::vector<Joy::User> friendList = GetFriendList(userid);

        Joy::ResultCode* code = response->mutable_result();
        code->set_errcode(0);
        code->set_errmsg("");
        for(Joy::User& user : friendList)
        {
            Joy::User* p = response->add_friendlist();
            *p = user;
        }

        done->Run();
    }
};

int main(int argc, char** argv)
{
    LOG_INFO("frist log messge!");
    LOG_ERROR("%s:%s:%d", __FILE__, __FUNCTION__, __LINE__);

    // 调用框架的初始化操作 provider -i config.conf
    MprpcApplication::Init(argc, argv);

    // provider是一个rpc网络服务对象，把UserServcie对象发布到rpc节点上
    RpcProvider provider; 
    provider.NotifyService(new FriendService());

    // 启动一个rpc服务发布节点，Run以后，进程进入阻塞状态，等待远程的rpc调用请求
    provider.Run();

    return 0;
}